package com.whfc.ms.api.fmam.controller;

import com.whfc.common.result.*;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fmam.entity.dto.FmamSupplierDTO;
import com.whfc.fmam.entity.qo.SupplierAddQO;
import com.whfc.fuum.dto.AppCorpDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.AppCorpListQO;
import com.whfc.ms.api.fmam.service.SupplierService;
import com.whfc.ms.config.AbstractBaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ；likang
 * @Description: 供料/发料单位接口
 * @Date:Create：in 2019/11/14 18:16
 * @Version：1.0
 */
@RequiresPermissions("fmam/corp")
@RestController
@RequestMapping("/ms/api/fmam/supplier")
public class FmamSupplierController extends AbstractBaseController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 供料/收料单位列表
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param category
     * @param deptId
     * @return
     */
    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result listSupplier(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize,
                               @RequestParam("category") Integer category,
                               @RequestParam(value = "deptId") Integer deptId) {
        logger.info("仓库列表页，loginUser:{},pageNum:{},pageSize:{},category:{},deptId:{}", user.getUsername(), pageNum, pageSize, category, deptId);
        PageData<FmamSupplierDTO> pageData = supplierService.listFmamSupplierDTO(user, pageNum, pageSize, category, deptId);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("category") Integer category) {
        logger.info("获取所有的收发料单位,loginer:{},deptId:{},category:{}", user.getUsername(), deptId, category);
        ListData<FmamSupplierDTO> listData = supplierService.list(deptId, category);
        return ResultUtil.success(listData);
    }

    /**
     * 查看供料/收料单位详情
     *
     * @param
     * @param id
     * @return
     */
    @PostMapping(value = "/detail/{id}")
    public Result detailSupplier(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @PathVariable("id") Integer id) {
        logger.info("查看供料/发料单位详情，loginUser:{},data:{}", user.getUsername(), id);
        FmamSupplierDTO fmamSupplierDTO = supplierService.supplierDetail(id);
        return ResultUtil.success(fmamSupplierDTO);
    }

    /**
     * 移出供料/收料单位
     *
     * @param
     * @param id
     * @return
     */
    @RequiresPermissions("fmam/corp/del")
    @PostMapping(value = "/del/{id}")
    public Result delSupplier(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @PathVariable("id") Integer id) {
        logger.info("移除供料/发料单位，loginer:{},data:{}", user.getUsername(), id);
        supplierService.deleteSupplier(id);
        return ResultUtil.success();
    }

    /**
     * 选择合作单位列表（去重显示）
     *
     * @param
     * @param request
     * @return
     */
    @PostMapping(value = "/corpList")
    public Result listCorp(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @Validated @RequestBody AppCorpListQO request) {
        logger.info("选择合作单位列表，loginer:{},data:{}", user.getUsername(), request.toString());
        PageData<AppCorpDTO> appCorpDTOPageData = supplierService.listCorp(user, request);
        return ResultUtil.success(appCorpDTOPageData);
    }

    /**
     * 添加物资合作单位
     *
     * @param user
     * @param supplierAddQO
     * @return
     */
    @RequiresPermissions("fmam/corp/add")
    @PostMapping(value = "/add")
    public Result addSupplier(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestBody @Validated SupplierAddQO supplierAddQO) {
        logger.info("添加物资合作单位，loginer:{},supplierAddQO:{}", user.getUsername(), supplierAddQO.toString());
        Integer category = supplierAddQO.getCategory();
        List<Integer> corpIds = supplierAddQO.getCorpIds();
        supplierService.addSupplier(category, corpIds);
        return ResultUtil.success();
    }

    /**
     * 2.0批量移出供料/收料单位
     *
     * @param idList
     * @return
     */
    @RequiresPermissions("fmam/corp/del")
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestBody List<Integer> idList) {
        logger.info("批量移出供料/收料单位,userId:{},idList:{}", getLoginUser().getId(), idList);
        AssertUtil.isTrue(CollectionUtils.isEmpty(idList), "ID不能为空");
        supplierService.deleteBatch(idList);
        return R.success();
    }
}
